(() => {
    const btn = document.createElement("button");
    btn.style.position = "fixed";
    btn.style.right = "100px";
    btn.style.bottom = "100px";
    const input = document.createElement("input");
    input.style.position = "fixed";
    input.style.right = "100px";
    input.style.bottom = "70px";
    document.body.appendChild(input);
    document.body.appendChild(btn);
    input.value = window.localStorage.getItem("speed") || "10-20"
    btn.innerText = "开始检测";
    const getSpeed = () => {
        const val = input.value;
        const [a, b] = val.split("-").map(e => parseInt(e));
        if (isNaN(a) || isNaN(b) || a < 1 || b < 1 || b < a) {
            input.value = "10-20";
            window.localStorage.setItem("speed", input.value);
        }
        return [a, b];
    }
    const getSubmitButton = () => {
        const btns = Array.from(document.getElementsByTagName("button"));
        return btns.find(e => e.innerText == "全部提交");
    }
    const getData = () => {
        const cards = Array.from(document.getElementsByClassName("one-div one-div-height"))
        return cards.map((card) => {
            const id = parseInt(card.id);
            const footer = card.getElementsByClassName("one-div-width")[0];
            const options = Array.from(footer.getElementsByTagName("button"));
            const type = card.getElementsByClassName("history-value-width")[0].innerText;
            return { id, options, card, type };
        })
    }
    let time;
    let flag = parseInt(window.localStorage.getItem("flag"));
    const hash = new Set();
    const submit = () => {
        if (!flag) return;
        const button = getSubmitButton();
        button.click();
        setTimeout(() => {
            document.getElementById("myModal").getElementsByClassName("btn btn-primary")[0].click();
        }, 500);
    }
    const getRequest = () => {
        if (!flag) return;
        const [speedA, speedB] = getSpeed();
        time = setTimeout(() => {
            let has = true;
            try {
                const data = getData();
                data.forEach(({ id, options, card, type }) => {
                    if (hash.has(id)) return;
                    has = false;
                    const option = options.find(e => e.innerText === type);
                    if (!option) return;
                    option.click();
                });
                setTimeout(() => {
                    submit();
                }, (Math.random() * (speedB - speedA) + speedA) * 1000)
            } catch (e) {//失败时重新启动
                return getRequest();
            }
            if (has) getRequest();//未获取到数据时
        }, 1000);
    }
    const onSubmit = () => {
        if (!current) return;
        setTimeout(() => {
            getRequest();
        }, 1000);
    }
    const start = () => {
        btn.innerText = "取消";
        const submitButton = getSubmitButton();
        getRequest();
        submitButton.addEventListener("click", onSubmit)
    }

    const close = () => {
        btn.innerText = "开始检测";
        clearTimeout(time);
        const submitButton = getSubmitButton();
        submitButton.removeEventListener("click", onSubmit)
    }
    if (flag) start();
    else close();

    btn.onclick = () => {
        flag = !flag;
        window.localStorage.setItem("flag", +flag);
        if (flag) start();
        else close();
    }
})()